package com.jcodecraeer.xrecyclerview.progressindicator.indicator;

import com.jcodecraeer.xrecyclerview.utils.DeviceUtils;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;

import java.util.ArrayList;
import java.util.List;

public class SysProgressIndicator extends BaseIndicatorController{
    private float scaleFloat=1;
    private float degrees;
    private Arc mArc = new Arc();//弧度

    @Override
    public void draw(Canvas canvas, Paint paint) {
        paint.setStyle(Paint.Style.STROKE_STYLE);
        paint.setStrokeWidth(DeviceUtils.dp2px(getTarget().getContext(),2));
        float x = (getWidth()) / 3;
        float y=(getHeight()) / 3;
        canvas.translate(x, y);
//        canvas.scale(scaleFloat, scaleFloat);
        canvas.rotate(degrees*360,0,0);
        RectFloat rectF=new RectFloat(-x,-y+5,
                x,y);
        mArc.setArc( 0, 270, false);
        canvas.drawArc(rectF,mArc,paint);
    }

    @Override
    public List<Animator> createAnimation() {
        List<Animator> animators=new ArrayList<>();
        AnimatorValue scaleAnim = new AnimatorValue();
        scaleAnim.setDuration(750);
        scaleAnim.setLoopedCount(-1);
        scaleAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                scaleFloat = v;
                postInvalidate();
            }
        });
        scaleAnim.start();

        AnimatorValue rotateAnim = new AnimatorValue();
        rotateAnim.setDuration(750);
        rotateAnim.setLoopedCount(-1);
        rotateAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                degrees = v;
                postInvalidate();
            }
        });
        rotateAnim.start();
        animators.add(scaleAnim);
        animators.add(rotateAnim);
        return animators;
    }
}
